package com.xthh._function;


import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.function.Function;

/**
 * Author: Simon
 * Time: @2023/03/11 20:17:19
 */
public class HH_Faker {
    // 判断是否为文件
    public static final Function<File, Boolean> Function_isFile = File::isFile;
    private final ForkJoinPool forkjoinPool;

    public HH_Faker() {
        this(Runtime.getRuntime().availableProcessors()-1);
    }
    public HH_Faker(int threshold) {
        forkjoinPool = new ForkJoinPool(threshold);
    }

    /**
     * 获取所有文件
     *
     * @param rootFile 根文件夹
     * @return 文件集合
     * @throws ExecutionException   执行出错
     * @throws InterruptedException 执行被打断
     */
    public ArrayList<File> getAllFile(File rootFile) throws ExecutionException, InterruptedException {
        FakerTask task = new FakerTask(rootFile, Function_isFile);
        //执行一个任务
        Future<ArrayList<File>> result = forkjoinPool.submit(task);

        return result.get();
    }

    /**
     * 处理所有文件/文件夹
     *
     * @param rootFile 根文件夹
     * @param function 处理方法
     * @return 处理后的文件/文件夹
     * @throws ExecutionException   执行出错
     * @throws InterruptedException 执行被打断
     */
    public ArrayList<File> computeAllFile(File rootFile, Function<File, Boolean> function) throws ExecutionException, InterruptedException {
        FakerTask task = new FakerTask(rootFile, function);
        //执行一个任务
        Future<ArrayList<File>> result = forkjoinPool.submit(task);

        return result.get();
    }


    private static class FakerTask extends RecursiveTask<ArrayList<File>> {
        private final File rootFile;
        private final Function<File, Boolean> function;

        public FakerTask(File rootFile, Function<File, Boolean> function) {
            this.rootFile = rootFile;
            this.function = function;
        }

        @Override
        protected ArrayList<File> compute() {
            // 如果是文件，直接返回
            if (function.apply(rootFile)) {
                ArrayList<File> files = new ArrayList<>();
                files.add(rootFile);
                return files;
            }
            ArrayList<File> bak = new ArrayList<>();

            File[] files = rootFile.listFiles();


            if (files == null) {
                // 这里是 极 特 殊 情 况
                // 某种特殊的文件会出现这种问题
                return null;
            }

            List<FakerTask> listTasks = new ArrayList<>();
            for (int i = 0; i < files.length; i++){
                // 去掉隐藏文件
                if (!files[i].isHidden()){
                    FakerTask ft = new FakerTask(files[i],function);
                    ft.fork();
                    listTasks.add(ft);
                }
            }
            for (int i = 0; i < listTasks.size(); i++){
                ArrayList<File> join = listTasks.get(i).join();
                if (join != null)
                    bak.addAll(join);
            }


          /*  FakerTask[] tasks = new FakerTask[files.length];
            // 逐个执行
            for (int i = 0; i < files.length; i++) {
                tasks[i] = new FakerTask(files[i],function);
                tasks[i].fork();
            }
            // 获取子任务的结果
            for (FakerTask task : tasks) {
                ArrayList<File> join = task.join();
                if (join != null)
                    bak.addAll(join);
            }*/


            return bak;
        }
    }
}

